package com.hzqy.service.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.Base64;
import com.hzqy.persistence.dao.statistics.ActivityDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.AwardUserVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.travel.TravelUserVo;

@Service
public class ActivityServiceBean extends GenericServiceBean<PmsUserVo, ActivityDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private ActivityDao activityDao;

	@Override
	public ActivityDao getEntityDao() {
		return activityDao;
	}
	
	public Map<String,Object> selectWinningList(AwardUserVo awardUserVo, int curPage,
			int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = activityDao.selectAwardUserCount(awardUserVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_au_serverid", awardUserVo.getF_au_serverid());
			map.put("f_au_phone", awardUserVo.getF_au_phone());
			map.put("f_au_grade", awardUserVo.getF_au_grade());
			map.put("f_au_activityid", awardUserVo.getF_au_activityid());
			map.put("f_au_type", awardUserVo.getF_au_type());
			map.put("beginDate", awardUserVo.getBeginDate());
			map.put("endDate", awardUserVo.getEndDate());
			List<AwardUserVo> results = activityDao.selectAwardUserList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<AwardUserVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectExcelList(AwardUserVo awardUserVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = activityDao.selectExcelCount(awardUserVo);
		map.put("total", rowCount);
		if(rowCount > 0) {
			map.put("f_au_serverid", awardUserVo.getF_au_serverid());
			map.put("f_au_userphone", awardUserVo.getF_au_phone());
			map.put("f_au_grade", awardUserVo.getF_au_grade());
			map.put("f_au_activityid", awardUserVo.getF_au_activityid());
			map.put("f_au_type", awardUserVo.getF_au_type());
			map.put("beginDate", awardUserVo.getBeginDate());
			map.put("endDate", awardUserVo.getEndDate());
			List<AwardUserVo> results = activityDao.selectExcelList(map);
			for(AwardUserVo auv : results){
				if(auv.getF_au_phone() != null && !"".equals(auv.getF_au_phone()) && !isPhone(auv.getF_au_phone())){
					byte[] tokenkey = Base64.decode(auv.getF_au_phone());
					String phone = new String(tokenkey);
					auv.setF_au_phone(phone);
				}
			}
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<AwardUserVo>());
		}
		return map;
	}
	//判断手机号
	public static boolean isPhone(String phone) {
    	String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        boolean isMatch = m.matches();
        return isMatch;
	}
}
